﻿using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Logging.Console;
using System;
using System.Collections.Generic;
using System.Text;

namespace DotNetExtensions.Core.Logging
{
    public static class LoggingExtensions
    {
        public static void AddProvider<TProvider,TProviderOptions>(this ILoggingBuilder builder) where TProvider: class, ILoggerProvider where TProviderOptions : class
        {
            builder.SetMinimumLevel(LogLevel.Debug);
            builder.AddFilter("Microsoft", LogLevel.Warning)
            .AddFilter("System", LogLevel.Warning)
            .AddFilter<ConsoleLoggerProvider>("DotNetExtensions.Core.Logging", LogLevel.Debug)
            .AddFilter<TProvider>("DotNetExtensions.Core.Logging", LogLevel.Debug);

            builder.AddConfiguration();
            builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, TProvider>());
            LoggerProviderOptions.RegisterProviderOptions<TProviderOptions, TProvider>(builder.Services);

            builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, ConsoleLoggerProvider>());
            LoggerProviderOptions.RegisterProviderOptions<ConsoleLoggerOptions, ConsoleLoggerProvider>(builder.Services);
        }
    }
}
